# -*- encoding: utf-8 -*-

from Common.mysql import db
from Common.public_method import utc_8
from src.Bugs.Database.bugs_database import BugData
from sqlalchemy.exc import SQLAlchemyError
from Common.yaml_method import YamlMethod


class DeleteBug:
    """
    删除缺陷相关接口
    """

    @staticmethod
    def delete_bug(bug_id, update_user):
        """
        删除缺陷
        :param bug_id: 缺陷ID
        :param update_user:
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        # 判断缺陷是否存在
        bug = BugData.query.filter_by(id=bug_id).first()
        if bug:
            bug.delete = 1
            bug.update_user = update_user
            update_time = utc_8()
            bug.update_time = update_time
            try:
                db.session.commit()
            except SQLAlchemyError:
                db.session.rollback()
                res = {
                    'code': code[6],
                    'data': [],
                    'message': '缺陷删除失败'
                }
                return res
            res = {
                'code': code[0],
                'data': [],
                'message': '缺陷删除成功'
            }
            return res
        else:
            res = {
                'code': code[3],
                'data': [],
                'message': '缺陷不存在'
            }
            return res
